package com.xjscrm.console.chat.core.impl.conversation;

import com.xjscrm.common.exception.BusinessException;
import com.xjscrm.console.chat.core.ChatBaseMsgHandler;
import com.xjscrm.console.chat.enums.ChatMsgType;
import com.xjscrm.console.chat.frame.recv.RConversationSilenceMsg;
import com.xjscrm.console.chat.frame.send.SResultMsg;
import com.xjscrm.console.entity.ChatTalkSession;
import com.xjscrm.console.entity.ChatTalkSessionStaffRel;
import com.xjscrm.console.service.customer.chat.ChatTalkSessionService;
import com.xjscrm.console.service.customer.chat.ChatTalkSessionStaffRelService;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @author mingyang.chen create in 2024/3/17 09:42
 */
@Component
@Slf4j
public class ChatConversationSilenceMsgHandler extends ChatBaseMsgHandler<RConversationSilenceMsg> {

    @Autowired
    private ChatTalkSessionStaffRelService chatTalkSessionStaffRelService;

    @Autowired
    private ChatTalkSessionService chatTalkSessionService;

    @Override
    public Class<RConversationSilenceMsg> msgClass() {
        return RConversationSilenceMsg.class;
    }

    @Override
    public boolean checkMsg(RConversationSilenceMsg baseMsg) {
        return super.checkMsg(baseMsg) || !ObjectUtils.isNotEmpty(baseMsg.getTalkSessionId()) || Objects.isNull(baseMsg.getOpen());
    }

    @Override
    public SResultMsg doHandle(RConversationSilenceMsg content, ChannelHandlerContext ctx) {

        ChatTalkSession byId = chatTalkSessionService.getById(content.getMerId(), content.getTalkSessionId());
        if (Objects.isNull(byId)) {
            throw new BusinessException("会话不存在: 开启免打扰失败");
        }
        ChatTalkSessionStaffRel byConversationId = chatTalkSessionStaffRelService.getBySessionId(content.getMerId(), content.getStaffId(), byId.getDeviceUniqueId(), byId.getId());
        if (Objects.isNull(byConversationId)) {
            throw new BusinessException("会话开启免打扰失败: 当前员工未拥有此会话");
        }
        if (content.getOpen()) {
            ChatTalkSessionStaffRel update = new ChatTalkSessionStaffRel();
            update.setId(byConversationId.getId());
            update.setMerId(byConversationId.getMerId());
            update.setOpenSilence(1);
            update.setOpenSilenceTime((int) (System.currentTimeMillis() / 1000L));
            update.setUpdateTime((int) (System.currentTimeMillis() / 1000L));

            chatTalkSessionStaffRelService.updateById(update);
            return SResultMsg.success(content, "开启免打扰成功");
        }
        //
        else {
            ChatTalkSessionStaffRel update = new ChatTalkSessionStaffRel();
            update.setId(byConversationId.getId());
            update.setMerId(byConversationId.getMerId());
            update.setOpenSilence(0);
            update.setOpenSilenceTime(-1);
            update.setUpdateTime((int) (System.currentTimeMillis() / 1000L));

            chatTalkSessionStaffRelService.updateById(update);
            return SResultMsg.success(content, "关闭免打扰成功");
        }
    }

    @Override
    public ChatMsgType msgType() {
        return ChatMsgType.r_conversation_silence;
    }
}
